<?php

class Stat_tradeApp extends BackendApp {

    function __construct() {
        $this->Stat_storeApp();
    }

    function Stat_storeApp() {
        parent::__construct();
        import('datehelper.lib');
        import('statistics.lib');
        $this->import_resource(
                array(
                    'script' => 'highcharts/highcharts.js,statistics.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                    'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css')
        );
        $this->search_arr = $_REQUEST;
        $this->assign('search_arr', $this->search_arr);
    }

    function goods() {

        if (!$_REQUEST['search_type']) {
            $_REQUEST['search_type'] = 'day';
        }
        //初始化时间
        //天
        if (!$_REQUEST['search_time']) {
            $_REQUEST['search_time'] = date('Y-m-d', gmtime());
        }
        $search_time = strtotime($_REQUEST['search_time']); //搜索的时间
        $this->assign('search_time', $_REQUEST['search_time']);
        //周
        if (!$_REQUEST['search_time_year']) {
            $_REQUEST['search_time_year'] = date('Y', gmtime());
        }
        if (!$_REQUEST['search_time_month']) {
            $_REQUEST['search_time_month'] = date('m', gmtime());
        }
        if (!$_REQUEST['search_time_week']) {
            $_REQUEST['search_time_week'] = implode('|', getWeek_SdateAndEdate(gmtime()));
        }
        $current_year = $_REQUEST['search_time_year'];
        $current_month = $_REQUEST['search_time_month'];
        $current_week = $_REQUEST['search_time_week'];
        $year_arr = getSystemYearArr();
        $month_arr = getSystemMonthArr();
        $week_arr = getMonthWeekArr($current_year, $current_month);

        $this->assign('current_year', $current_year);
        $this->assign('current_month', $current_month);
        $this->assign('current_week', $current_week);
        $this->assign('year_arr', $year_arr);
        $this->assign('month_arr', $month_arr);
        $this->assign('week_arr', $week_arr);
        if ($_REQUEST['search_type'] == 'day') {
            $stime = $search_time; //昨天0点
            $etime = $search_time + 86400 - 1; //今天24点
            $this->assign('actionurl', 'index.php?' . $_SERVER['QUERY_STRING']);
        }
        if ($_REQUEST['search_type'] == 'week') {
            $current_weekarr = explode('|', $current_week);
            $stime = strtotime($current_weekarr[0]) - 86400 * 7;
            $etime = strtotime($current_weekarr[1]) + 86400 - 1;
            $this->assign('actionurl', 'index.php?' . $_SERVER['QUERY_STRING']);
        }
        if ($_REQUEST['search_type'] == 'month') {
            $stime = strtotime($current_year . '-' . $current_month . "-01 0 month");
            $etime = getMonthLastDay($current_year, $current_month) + 86400 - 1;
            $this->assign('actionurl', 'index.php?' . $_SERVER['QUERY_STRING']);
        }
        /*
         * 获取数据
         */
        $conditions = "add_time>{$stime} AND add_time < {$etime}";
        //获取上架商品数量
        $goods_mod = & m('goods');
        $this->assign('goods_on_allnum', $goods_mod->getOne("SELECT count(*) as allnum FROM {$goods_mod->table} WHERE " . $conditions));
        //下单商品数
        $order_mod = & m('order');
        $order_goods_mod = & m('ordergoods');
        $this->assign('order_goods_num', $order_goods_mod->getOne("SELECT sum(quantity) as allnum FROM {$order_goods_mod->table} LEFT JOIN {$order_mod->table} ON {$order_goods_mod->table}.order_id={$order_mod->table}.order_id WHERE " . $conditions));

        //下单单量
        $this->assign('order_num', $order_mod->getOne("SELECT count(*) as allnum FROM {$order_mod->table} WHERE " . $conditions));
        //下单客户数
        $this->assign('order_buyer_num', $order_mod->getOne("SELECT DISTINCT(buyer_id)  FROM {$order_mod->table} WHERE " . $conditions));
        //合计金额
        $this->assign('order_amount', $order_mod->getOne("SELECT sum(goods_amount) as allnum FROM {$order_mod->table} WHERE " . $conditions));

        //获取商品销售排名TOP15数据

        switch (trim($_GET['rank_type'])) {
            case '':
            case 'trade_num':
                $goods_list = $order_goods_mod->find(array(
                    'fields' => ' sum(quantity) as allnum,goods_name,goods_id ',
                    'conditions' => $conditions . " group by goods_id",
                    'order' => 'allnum desc',
                    'join' => 'belongs_to_order',
                    'limit' => 15,
                ));
                $this->assign('table_tip', '销量');
                $this->assign('chart_tip', '商品销量排行TOP15');
                break;
            case 'trade_amount':
                $goods_list = $order_goods_mod->find(array(
                    'fields' => 'sum(quantity*price) as allnum,goods_name,goods_id ',
                    'conditions' => $conditions . " group by goods_id",
                    'order' => 'allnum desc',
                    'join' => 'belongs_to_order',
                    'limit' => 15,
                ));
                $this->assign('table_tip', '销售额');
                $this->assign('chart_tip', '商品销售额排行TOP15');
                break;
            case 'trade_users':
                $goods_list = $order_goods_mod->find(array(
                    'fields' => 'sum(quantity*price) as allnum,goods_name,goods_id ',
                    'conditions' => $conditions . " group by goods_id",
                    'order' => 'allnum desc',
                    'join' => 'belongs_to_order',
                    'limit' => 15,
                ));
                $this->assign('table_tip', '成交用户数');
                $this->assign('chart_tip', '商品销售额排行TOP15');
                break;
        }
        $goods_list = array_merge($goods_list);

        if ($_GET['exporttype'] == 'excel') {
            //导出Excel
            import('excel.lib');
            $excel_obj = new Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
            //header
            $excel_data[0][0] = array('styleid' => 's_title', 'data' => '排名');
            $excel_data[0][1] = array('styleid' => 's_title', 'data' => '商品名称');
            $excel_data[0][2] = array('styleid' => 's_title', 'data' => trim($_GET['rank_type']) == 'trade_amount' ? '销售额' : '销量');
            //data
            foreach ($goods_list as $k => $v) {
                $excel_data[$k + 1][0] = array('data' => $k + 1);
                $excel_data[$k + 1][1] = array('data' => $v['goods_name']);
                $excel_data[$k + 1][2] = array('data' => $v['allnum']);
            }
            $excel_data = $excel_obj->charset($excel_data, CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset(trim($_GET['rank_type']) == 'trade_amount' ? '商品销售额排行TOP15 ' : '商品销量排行TOP15 ', CHARSET));
            $excel_obj->generateXML($excel_obj->charset(trim($_GET['rank_type']) == 'trade_amount' ? '商品销售额排行TOP15 ' : '商品销量排行TOP15 ', CHARSET) . date('Y-m-d-H', gmtime()));
            exit();
        } else {
            //构造横轴数据
            for ($i = 1; $i <= 15; $i++) {
                //横轴
                $stat_arr['xAxis']['categories'][] = $i;
            }
            $stat_arr['title'] = trim($_GET['rank_type']) == 'trade_amount' ? '商品销售额排行TOP15 ' : '商品销量排行TOP15 ';
            $stat_arr['yAxis'] = trim($_GET['rank_type']) == 'trade_amount' ? '销售额' : '销量';
            $stat_arr['series'][0]['name'] = trim($_GET['rank_type']) == 'trade_amount' ? '销售额' : '销量';
            $stat_arr['series'][0]['data'] = array();
            for ($i = 0; $i < 15; $i++) {
                $stat_arr['series'][0]['data'][] = array('name' => strval($goods_list[$i]['goods_name']), 'y' => floatval($goods_list[$i]['allnum']));
            }
            $stat_arr['legend']['enabled'] = false;
            $stat_json = getStatData_Column2D($stat_arr);
            $this->assign('stat_json', $stat_json);
            $this->assign('goods_list', $goods_list);
            $this->_curmenu('stat_trade_goods');
            $this->display('stat_trade.goods.html');
        }
    }

    function account() {
        $account_log_mod = & m('account_log');
        $conditions = "complete=1";
        $conditions .= $this->_get_query_conditions(
                array(
                    array(
                        'field' => 'type',
                        'equal' => '=',
                        'name' => 'type',
                        'type' => 'numeric',
                    ),
                )
        );
        $money_flow = $_GET['money_flow'];
        if (!in_array($money_flow, array('income', 'outlay'))) {
            $money_flow = 'income';
        }
        $conditions .= " AND money_flow='{$money_flow}'";

        if (!$_REQUEST['search_type']) {
            $_REQUEST['search_type'] = 'day';
        }
        //初始化时间
        //天
        if (!$_REQUEST['search_time']) {
            $_REQUEST['search_time'] = date('Y-m-d', gmtime());
        }
        $search_time = strtotime($_REQUEST['search_time']); //搜索的时间
        $this->assign('search_time', $_REQUEST['search_time']);
        //周
        if (!$_REQUEST['search_time_year']) {
            $_REQUEST['search_time_year'] = date('Y', gmtime());
        }
        if (!$_REQUEST['search_time_month']) {
            $_REQUEST['search_time_month'] = date('m', gmtime());
        }
        if (!$_REQUEST['search_time_week']) {
            $_REQUEST['search_time_week'] = implode('|', getWeek_SdateAndEdate(gmtime()));
        }
        $current_year = $_REQUEST['search_time_year'];
        $current_month = $_REQUEST['search_time_month'];
        $current_week = $_REQUEST['search_time_week'];
        $year_arr = getSystemYearArr();
        $month_arr = getSystemMonthArr();
        $week_arr = getMonthWeekArr($current_year, $current_month);

        $this->assign('current_year', $current_year);
        $this->assign('current_month', $current_month);
        $this->assign('current_week', $current_week);
        $this->assign('year_arr', $year_arr);
        $this->assign('month_arr', $month_arr);
        $this->assign('week_arr', $week_arr);

        $statlist = array(); //统计数据列表
        if ($_REQUEST['search_type'] == 'day') {
            //构造横轴数据
            for ($i = 0; $i < 24; $i++) {
                //统计图数据
                $curr_arr[$i] = 0; //今天
                $up_arr[$i] = 0; //昨天
                //统计表数据
                $uplist_arr[$i]['timetext'] = $i;
                $currlist_arr[$i]['timetext'] = $i;
                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = "$i";
            }
            $stime = $search_time - 86400; //昨天0点
            $etime = $search_time + 86400 - 1; //今天24点

            $today_day = @date('d', $search_time); //今天日期
            $yesterday_day = @date('d', $stime); //昨天日期


            $conditions .= " AND add_time>{$stime} AND add_time < {$etime}";
            $memberlist = $account_log_mod->find(array(
                'fields' => 'sum(money) as allnum,DAY(FROM_UNIXTIME(add_time)) as dayval,HOUR(FROM_UNIXTIME(add_time)) as hourval ',
                'conditions' => $conditions . " group by dayval,hourval",
                'order' => "add_time desc",
                    )
            );

            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($today_day == $v['dayval']) {
                        $curr_arr[$v['hourval']] = abs($v['allnum']);
                        $currlist_arr[$v['hourval']]['val'] = abs($v['allnum']);
                    }
                    if ($yesterday_day == $v['dayval']) {
                        $up_arr[$v['hourval']] = abs($v['allnum']);
                        $uplist_arr[$v['hourval']]['val'] = abs($v['allnum']);
                    }
                }
            }
            $stat_arr['series'][0]['name'] = '昨天';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '今天';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);

            //统计数据标题
            $statlist['headertitle'] = array('小时', '昨天', '今天', '同比');
            $this->assign('actionurl', 'index.php?' . $_SERVER['QUERY_STRING']);
        }

        if ($_REQUEST['search_type'] == 'week') {
            $current_weekarr = explode('|', $current_week);
            $stime = strtotime($current_weekarr[0]) - 86400 * 7;
            $etime = strtotime($current_weekarr[1]) + 86400 - 1;
            $up_week = @date('W', $stime); //上周
            $curr_week = @date('W', $etime); //本周
            //构造横轴数据
            for ($i = 1; $i <= 7; $i++) {
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                $tmp_weekarr = getSystemWeekArr();
                //统计表数据
                $uplist_arr[$i]['timetext'] = $tmp_weekarr[$i];
                $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
                unset($tmp_weekarr);
            }


            $conditions .= " AND add_time>{$stime} AND add_time < {$etime}";
            $memberlist = $account_log_mod->find(array(
                'fields' => 'sum(money) as allnum,WEEKOFYEAR(FROM_UNIXTIME(add_time)) as weekval,WEEKDAY(FROM_UNIXTIME(add_time))+1 as dayofweekval ',
                'conditions' => $conditions . " group by  weekval,dayofweekval",
                'limit' => $page['limit'],
                'order' => "add_time desc",
                    )
            );


            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($up_week == $v['weekval']) {
                        $up_arr[$v['dayofweekval']] = abs($v['allnum']);
                        $uplist_arr[$v['dayofweekval']]['val'] = abs($v['allnum']);
                    }
                    if ($curr_week == $v['weekval']) {
                        $curr_arr[$v['dayofweekval']] = abs($v['allnum']);
                        $currlist_arr[$v['dayofweekval']]['val'] = abs($v['allnum']);
                    }
                }
            }
            $stat_arr['series'][0]['name'] = '上周';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '本周';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            //统计数据标题
            $statlist['headertitle'] = array('星期', '上周', '本周', '同比');
            $this->assign('actionurl', 'index.php?' . $_SERVER['QUERY_STRING']);
        }

        if ($_REQUEST['search_type'] == 'month') {
            $stime = strtotime($current_year . '-' . $current_month . "-01");
            $etime = getMonthLastDay($current_year, $current_month) + 86400 - 1;

            $up_month = date('m', $stime);
            $curr_month = date('m', $etime);
            //计算横轴的最大量（由于每个月的天数不同）
            $up_dayofmonth = date('t', $stime);
            $curr_dayofmonth = date('t', $etime);
            $x_max = $up_dayofmonth > $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;

            //构造横轴数据
            for ($i = 1; $i <= $x_max; $i++) {
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                //统计表数据
                $uplist_arr[$i]['timetext'] = $i;
                $currlist_arr[$i]['timetext'] = $i;
                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = $i;
            }


            $conditions .= " AND add_time>{$stime} AND add_time < {$etime}";
            $memberlist = $account_log_mod->find(array(
                'fields' => 'sum(money) as allnum ,MONTH(FROM_UNIXTIME(add_time)) as monthval,day(FROM_UNIXTIME(add_time)) as dayval ',
                'conditions' => $conditions . " group by monthval,dayval",
                'order' => "add_time desc",
                    )
            );



            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($up_month == $v['monthval']) {
                        $up_arr[$v['dayval']] = abs($v['allnum']);
                        $uplist_arr[$v['dayval']]['val'] = abs($v['allnum']);
                    }
                    if ($curr_month == $v['monthval']) {
                        $curr_arr[$v['dayval']] = abs($v['allnum']);
                        $currlist_arr[$v['dayval']]['val'] = abs($v['allnum']);
                    }
                }
            }
            $stat_arr['series'][0]['name'] = '上月';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '本月';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            //统计数据标题
            $statlist['headertitle'] = array('日期', '上月', '本月', '同比');
            $this->assign('actionurl', 'index.php?' . $_SERVER['QUERY_STRING']);
        }

        //计算同比
        foreach ((array) $currlist_arr as $k => $v) {
            $tmp = array();
            $tmp['timetext'] = $v['timetext'];
            $tmp['currentdata'] = $v['val'];
            $tmp['updata'] = $uplist_arr[$k]['val'];
            $tmp['tbrate'] = getTb($tmp['updata'], $tmp['currentdata']);
            $statlist['data'][] = $tmp;
        }

        $account_type_list = array(
            ACCOUNT_TYPE_ADMIN => Lang::get('account_type_admin'), //手工操作
            ACCOUNT_TYPE_BUY => Lang::get('account_type_buy'), //购买商品
            ACCOUNT_TYPE_SELLER => Lang::get('account_type_seller'), //出售商品
            ACCOUNT_TYPE_IN => Lang::get('account_type_in'), //账户转入
            ACCOUNT_TYPE_OUT => Lang::get('account_type_out'), //账户转出
            ACCOUNT_TYPE_CZ => Lang::get('account_type_cz'), //账户充值
            ACCOUNT_TYPE_TX => Lang::get('account_type_tx'), //账户提现
            ACCOUNT_TYPE_REFUND_IN => Lang::get('account_type_refund_in'), //账户退款收入,通常为买家退款成功 得到退款
            ACCOUNT_TYPE_REFUND_OUT => Lang::get('account_type_refund_out'), //账户退款收入,通常为卖家退款成功 扣除退款
            ACCOUNT_TYPE_TUIJIAN_BUYER => Lang::get('account_type_tuijian_buyer'), // 用户推荐注册,注册者购买产品，推荐人会获得佣金，店铺会损失佣金。
            ACCOUNT_TYPE_TUIJIAN_SELLER => Lang::get('account_type_tuijian_seller'), // 用户推荐注册,注册者成为店主，卖出产品推荐人会获得佣金，店主会损失佣金。
            ACCOUNT_TYPE_TRADE_CHARGES => Lang::get('account_type_trade_charges'), // 扣除卖家交易佣金
        );
		
		$money_flow_list = array(
                'income' => Lang::get('income'),
                'outlay' => Lang::get('outlay'),
            );
        //导出Excel
        if ($_GET['exporttype'] == 'excel') {
            //获取数据
            $log_list = $account_log_mod->find(array(
                'conditions' => $conditions,
                'order' => "add_time desc",
                    )
            );

            //导出Excel
            import('excel.lib');
            $excel_obj = new Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
            //header
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '会员名称');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '收入/支出');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '金额（元）');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '日期');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '类型');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '描述');
            //data
            foreach ($log_list as $k => $v) {
                $excel_data[$k + 1][] = array('data' => $v['user_name']);
                $excel_data[$k + 1][] = array('data' => $money_flow_list[$v['money_flow']]);
                $excel_data[$k + 1][] = array('data' => $v['money']);
                $excel_data[$k + 1][] = array('data' => date("Y-m-d H:i:s", $v['add_time']));
                $excel_data[$k + 1][] = array('data' => $account_type_list[$v['type']]);
                $excel_data[$k + 1][] = array('data' => $v['log_text']);
            }
            $excel_data = $excel_obj->charset($excel_data, CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset('预存款统计', CHARSET));
            $excel_obj->generateXML($excel_obj->charset('预存款统计', CHARSET) . date('Y-m-d-H', time()));
            exit();
        } else {
            $this->assign('account_type_list', $account_type_list);

            
            $this->assign('money_flow_list', $money_flow_list);

            $page = $this->_get_page(15);
            $log_list = $account_log_mod->find(array(
                'conditions' => $conditions,
                'order' => "add_time desc",
                'count' => true,
                'limit' => $page['limit'],
                    )
            );
            foreach ($log_list as $key => $value) {
                $log_list[$key]['type'] = $account_type_list[$value['type']];
                $log_list[$key]['money_flow'] = $money_flow_list[$value['money_flow']];
            }
            $this->assign('log_list', $log_list);
            $page['item_count'] = $account_log_mod->getCount();
            $this->_format_page($page);
            $this->assign('page_info', $page);

            //得到统计图数据
            $stat_arr['title'] = '预存款统计';
            $stat_arr['yAxis'] = '金额';
            $stat_json = getStatData_LineLabels($stat_arr);
            $this->assign('stat_json', $stat_json);
            $this->assign('statlist', $statlist);
            $this->_curmenu('stat_trade_account');
            $this->display('stat_trade.account.html');
        }
    }

    function sale() {
        $conditions = "1=1 ";
        $conditions .= $this->_get_query_conditions(
                array(
                    array(
                        'field' => 'status',
                        'equal' => '=',
                        'name' => 'status',
                        'type' => 'numeric',
                    ),
                    array(
                        'field' => 'seller_name',
                        'name' => 'seller_name',
                        'equal' => 'like',
                    ),
                )
        );

        if (trim($_GET['store_name']) != '') {
            $where['store_name'] = trim($_GET['store_name']);
            $store_name = trim($_GET['store_name']);
        }

        $group = '';
        if (trim($_GET['stat_type']) == 'sale_num') {
            $field = ' sum(order_amount) as allnum ,';
            $stat_arr['title'] = '订单量统计';
            $stat_arr['yAxis'] = '订单量';
        } elseif(trim($_GET['stat_type']) == 'sale_amount') {
            $field = ' count(*) as allnum ,';
            $stat_arr['title'] = '订单销售额统计';
            $stat_arr['yAxis'] = '订单销售额';
        } elseif(trim($_GET['stat_type']) == 'trade_users'){
            $field = ' count(*) as allnum ,buyer_id,';
            $group = ',buyer_id';
            $stat_arr['title'] = '成交用户数';
            $stat_arr['yAxis'] = '用户数';
        } else{
            $field = ' avg(order_amount) as allnum ,';
            $group = ',buyer_id';
            $stat_arr['title'] = '平均客单价';
            $stat_arr['yAxis'] = '单价';
        }

        $this->assign('info',$_GET['stat_type']?$_GET['stat_type']:'sale_num');
        if (!$_REQUEST['search_type']) {
            $_REQUEST['search_type'] = 'day';
        }
        //初始化时间
        //天
        if (!$_REQUEST['search_time']) {
            $_REQUEST['search_time'] = date('Y-m-d', gmtime());
        }
        $search_time = strtotime($_REQUEST['search_time']); //搜索的时间
        $this->assign('search_time', $_REQUEST['search_time']);
        //周
        if (!$_REQUEST['search_time_year']) {
            $_REQUEST['search_time_year'] = date('Y', gmtime());
        }
        if (!$_REQUEST['search_time_month']) {
            $_REQUEST['search_time_month'] = date('m', gmtime());
        }
        if (!$_REQUEST['search_time_week']) {
            $_REQUEST['search_time_week'] = implode('|', getWeek_SdateAndEdate(gmtime()));
        }
        $current_year = $_REQUEST['search_time_year'];
        $current_month = $_REQUEST['search_time_month'];
        $current_week = $_REQUEST['search_time_week'];
        $year_arr  = getSystemYearArr();
        $month_arr = getSystemMonthArr();
        $week_arr  = getMonthWeekArr($current_year, $current_month);

        $this->assign('current_year', $current_year);
        $this->assign('current_month', $current_month);
        $this->assign('current_week', $current_week);
        $this->assign('year_arr', $year_arr);
        $this->assign('month_arr', $month_arr);
        $this->assign('week_arr', $week_arr);

        $order_mod = &m('order');
        $statlist = array(); //统计数据列表
        $sum_num = 0; //总数统计
        if ($_REQUEST['search_type'] == 'day') {
            //构造横轴数据
            for ($i = 0; $i < 24; $i++) {
                //统计图数据
                $curr_arr[$i] = 0; //今天
                $up_arr[$i] = 0; //昨天
                //统计表数据
                $uplist_arr[$i]['timetext'] = $i;
                $currlist_arr[$i]['timetext'] = $i;
                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = "$i";
            }
            $stime = $search_time - 86400; //昨天0点
            $etime = $search_time + 86400 - 1; //今天24点

            $today_day = @date('d', $search_time); //今天日期
            $yesterday_day = @date('d', $stime); //昨天日期



            $conditions .= " AND add_time>{$stime} AND add_time < {$etime}";
            $memberlist = $order_mod->find(array(
                'fields' => $field . ' DAY(FROM_UNIXTIME(add_time)) as dayval,HOUR(FROM_UNIXTIME(add_time)) as hourval ',
                'conditions' => $conditions . " group by dayval,hourval".$group,
            ));

            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($today_day == $v['dayval']) {
                        $curr_arr[$v['hourval']] = intval($v['allnum']);
                        $currlist_arr[$v['hourval']]['val'] = intval($v['allnum']);
                    }
                    if ($yesterday_day == $v['dayval']) {
                        $up_arr[$v['hourval']] = intval($v['allnum']);
                        $uplist_arr[$v['hourval']]['val'] = intval($v['allnum']);
                    }
                }
            } elseif (trim($_GET['store_name']) != '') {
                $this->assign('data_null', 'yes');
            }
            $stat_arr['series'][0]['name'] = '昨天';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '今天';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);

            $this->assign('actionurl', 'index.php?' . $_SERVER['QUERY_STRING']);
        }

        if ($_REQUEST['search_type'] == 'week') {
            $current_weekarr = explode('|', $current_week);
            $stime = strtotime($current_weekarr[0]) - 86400 * 7;
            $etime = strtotime($current_weekarr[1]) + 86400 - 1;
            $up_week = @date('W', $stime); //上周
            $curr_week = @date('W', $etime); //本周
            //构造横轴数据
            for ($i = 1; $i <= 7; $i++) {
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                $tmp_weekarr = getSystemWeekArr();
                //统计表数据
                $uplist_arr[$i]['timetext'] = $tmp_weekarr[$i];
                $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
                unset($tmp_weekarr);
            }

            $conditions .= " AND add_time>{$stime} AND add_time < {$etime}";
            $memberlist = $order_mod->find(array(
                'fields' => $field . ' WEEKOFYEAR(FROM_UNIXTIME(add_time)) as weekval,WEEKDAY(FROM_UNIXTIME(add_time))+1 as dayofweekval ',
                'conditions' => $conditions . " group by weekval,dayofweekval".$group,
            ));

            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($up_week == $v['weekval']) {
                        $up_arr[$v['dayofweekval']] = intval($v['allnum']);
                        $uplist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
                    }
                    if ($curr_week == $v['weekval']) {
                        $curr_arr[$v['dayofweekval']] = intval($v['allnum']);
                        $currlist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
                    }
                }
            } elseif (trim($_GET['store_name']) != '') {
                $this->assign('data_null', 'yes');
            }
            $stat_arr['series'][0]['name'] = '上周';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '本周';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);

            $this->assign('actionurl', 'index.php?' . $_SERVER['QUERY_STRING']);
        }

        if ($_REQUEST['search_type'] == 'month') {
            $stime = strtotime($current_year . '-' . $current_month . "-01 -1 month");
            $etime = getMonthLastDay($current_year, $current_month) + 86400 - 1;

            $up_month = date('m', $stime);
            $curr_month = date('m', $etime);
            //计算横轴的最大量（由于每个月的天数不同）
            $up_dayofmonth = date('t', $stime);
            $curr_dayofmonth = date('t', $etime);
            $x_max = $up_dayofmonth > $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;

            //构造横轴数据
            for ($i = 1; $i <= $x_max; $i++) {
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                //统计表数据
                $uplist_arr[$i]['timetext'] = $i;
                $currlist_arr[$i]['timetext'] = $i;
                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = $i;
            }

            $conditions .= " AND add_time>{$stime} AND add_time < {$etime}";
            $memberlist = $order_mod->find(array(
                'fields' => $field . ' MONTH(FROM_UNIXTIME(add_time)) as monthval,day(FROM_UNIXTIME(add_time)) as dayval ',
                'conditions' => $conditions . " group by monthval,dayval".$group,
            ));

            if ($memberlist) {
                foreach ($memberlist as $k => $v) {
                    if ($up_month == $v['monthval']) {
                        $up_arr[$v['dayval']] = intval($v['allnum']);
                        $uplist_arr[$v['dayval']]['val'] = intval($v['allnum']);
                    }
                    if ($curr_month == $v['monthval']) {
                        $curr_arr[$v['dayval']] = intval($v['allnum']);
                        $currlist_arr[$v['dayval']]['val'] = intval($v['allnum']);
                    }
                }
            } elseif (trim($_GET['store_name']) != '') {
                $this->assign('data_null', 'yes');
            }
            $stat_arr['series'][0]['name'] = '上月';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '本月';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);

            $this->assign('actionurl', 'index.php?' . $_SERVER['QUERY_STRING']);
        }
        //统计数据标题
        $statlist['headertitle'] = array('订单号','商品名称', '买家', '店铺名称', '支付时间', '订单金额（不含税）', '订单状态','发货地','跨境综合税');

        $order_status_list = array(
            ORDER_PENDING => Lang::get('order_pending'),
            ORDER_SUBMITTED => Lang::get('order_submitted'),
            ORDER_ACCEPTED => Lang::get('order_accepted'),
            ORDER_SHIPPED => Lang::get('order_shipped'),
            ORDER_FINISHED => Lang::get('order_finished'),
            ORDER_CANCELED => Lang::get('order_canceled'),
            ORDER_REFUND_SUCCESS => '退款成功'
        );

        $storestock = &m("storestock");
        $goodsspec = &m("goodsspec");
        //导出Excel
        if ($_GET['exporttype'] == 'excel') {
            //导出Excel
            import('excel.lib');
            $excel_obj = new Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
            //header
            foreach ($statlist['headertitle'] as $v) {
                $excel_data[0][] = array('styleid' => 's_title', 'data' => $v);
            }

            $order_all_list = $order_mod->find(
                    array
                        (
                        'join'          => 'has_ordergoods',
                        'fields' => 'order.*,ordergoods.goods_name,ordergoods.specification,ordergoods.price_amount,ordergoods.tax_price_amount,ordergoods.price_total,ordergoods.spec_id',
                        'conditions' => $conditions,
                        'order' => "add_time desc",
                    )
            );

            //data
            foreach ($order_all_list as $k => &$v) {
                $goodsspecInfo = $goodsspec->get(array("conditions"=>"spec_id = ".$v['spec_id']));
                $storestockInfo = $storestock->get(array("conditions"=>"storestock_id =".$goodsspecInfo['spec_3value']));
                $v['store_name'] = $storestockInfo['store_name'];


                $excel_data[$k + 1][] = array('data' => $v['order_sn']);
                $excel_data[$k + 1][] = array('data' => $v['goods_name'].':'.$v['specification']);
                $excel_data[$k + 1][] = array('data' => $v['buyer_name']);
                $excel_data[$k + 1][] = array('data' => $v['seller_name']);
                $status_value = date('Y-m-d H:i:s', $v['pay_time']);
                if($v['status'] == 0){
                    $status_value = '未支付';
                }
                $excel_data[$k + 1][] = array('data' =>$status_value);
                if($v['status'] == 50){
                    $excel_data[$k + 1][] = array('data' => '-'.number_format(($v['goods_amount']), 2));
                }else{
                    $excel_data[$k + 1][] = array('data' => number_format(($v['goods_amount']), 2));
                }
                $excel_data[$k + 1][] = array('data' => $order_status_list[$v['status']]);
                $excel_data[$k + 1][] = array('data' => $v['store_name']);
                $excel_data[$k + 1][] = array('data' => number_format(($v['tax_price_amount']), 2));
            }
            $excel_data = $excel_obj->charset($excel_data, CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset('订单统计', CHARSET));
            $excel_obj->generateXML($excel_obj->charset('订单统计', CHARSET) . date('Y-m-d-H', gmtime()));
            exit();
        } else {
            $this->assign('order_status_list', $order_status_list);

            $page = $this->_get_page(15);
            $order_list = $order_mod->find(
                    array
                        (
                        'fields' => 'order.*,ordergoods.goods_name,ordergoods.specification,ordergoods.price_amount,ordergoods.tax_price_amount,ordergoods.price_total,ordergoods.spec_id',
                        'join'          => 'has_ordergoods',
                        'conditions' => $conditions,
                        'limit' => $page['limit'],
                        'order' => "add_time desc",
                        'count' => true,
                    )
            );
            foreach ($order_list as $key => $value) {
                $order_list[$key]['status_value'] = $order_status_list[$value['status']];
                $goodsspecInfo = $goodsspec->get(array("conditions"=>"spec_id = ".$value['spec_id']));
                $storestockInfo = $storestock->get(array("conditions"=>"storestock_id =".$goodsspecInfo['spec_3value']));
                $order_list[$key]['store_name'] = $storestockInfo['store_name'];
            }
            $page['item_count'] = $order_mod->getCount();
            $this->_format_page($page);
            $this->assign('page_info', $page);
            $this->assign('order_list', $order_list);



            $stat_json = getStatData_LineLabels($stat_arr);
            $ordergoods = &m("ordergoods");
            //总数统计
            $amount = $order_mod->getOne("SELECT count(*) as allnum FROM {$order_mod->table} WHERE " . $conditions);
            $sale = $order_mod->getOne("SELECT sum(ecm_order_goods.price_amount) as allnum  FROM ecm_order  LEFT JOIN ecm_order_goods on ecm_order_goods.order_id = ecm_order.order_id  WHERE " . $conditions);
            $users = count($order_mod->getAll("SELECT * FROM {$order_mod->table} WHERE " . $conditions ." group by buyer_id"));
            $avg = $order_mod->getOne("SELECT CAST(AVG(order_amount) as decimal(10,2)) as allnum  FROM {$order_mod->table} WHERE " . $conditions );
            $this->assign('sum_data', array($amount,$sale, $users,$avg));
            $this->assign('stat_json', $stat_json);
            $this->assign('statlist', $statlist);
            $this->_curmenu('stat_trade_sale');
            $this->display('stat_trade.sale.html');
        }
    }

    function goods_sale() {
        if (!$_REQUEST['search_type']) {
            $_REQUEST['search_type'] = 'day';
        }
        //初始化时间
        //天
        if (!$_REQUEST['search_time']) {
            $_REQUEST['search_time'] = date('Y-m-d', time());
        }
        $search_time = strtotime($_REQUEST['search_time']); //搜索的时间
        $this->assign('search_time', $_REQUEST['search_time']);
        //周
        if (!$_REQUEST['search_time_year']) {
            $_REQUEST['search_time_year'] = date('Y', time());
        }
        if (!$_REQUEST['search_time_month']) {
            $_REQUEST['search_time_month'] = date('m', time());
        }
        if (!$_REQUEST['search_time_week']) {
            $_REQUEST['search_time_week'] = implode('|', getWeek_SdateAndEdate(time()));
        }
        $current_year = $_REQUEST['search_time_year'];
        $current_month = $_REQUEST['search_time_month'];
        $current_week = $_REQUEST['search_time_week'];
        $year_arr = getSystemYearArr();
        $month_arr = getSystemMonthArr();
        $week_arr = getMonthWeekArr($current_year, $current_month);

        $this->assign('current_year', $current_year);
        $this->assign('current_month', $current_month);
        $this->assign('current_week', $current_week);
        $this->assign('year_arr', $year_arr);
        $this->assign('month_arr', $month_arr);
        $this->assign('week_arr', $week_arr);
        if ($_REQUEST['search_type'] == 'day') {
            $stime = $search_time; //昨天0点
            $etime = $search_time + 86400 - 1; //今天24点
            $this->assign('actionurl', 'index.php?' . $_SERVER['QUERY_STRING']);
        }
        if ($_REQUEST['search_type'] == 'week') {
            $current_weekarr = explode('|', $current_week);
            $stime = strtotime($current_weekarr[0]) - 86400 * 7;
            $etime = strtotime($current_weekarr[1]) + 86400 - 1;
            $this->assign('actionurl', 'index.php?' . $_SERVER['QUERY_STRING']);
        }
        if ($_REQUEST['search_type'] == 'month') {
            $stime = strtotime($current_year . '-' . $current_month . "-01 0 month");
            $etime = getMonthLastDay($current_year, $current_month) + 86400 - 1;
            $this->assign('actionurl', 'index.php?' . $_SERVER['QUERY_STRING']);
        }


        $conditions = "1=1 AND add_time>{$stime} AND add_time < {$etime}";
        $conditions .= $this->_get_query_conditions(
                array(
                    array(
                        'field' => 'status',
                        'equal' => '=',
                        'name' => 'status',
                        'type' => 'numeric',
                    ),
                    array(
                        'field' => 'goods_name',
                        'equal' => 'like',
                    ),
                    array(
                        'field' => 'seller_name',
                        'equal' => 'like',
                    ),
                )
        );

        $order_goods_mod = &m('ordergoods');
        $order_status_list = array(
            ORDER_PENDING => Lang::get('order_pending'),
            ORDER_SUBMITTED => Lang::get('order_submitted'),
            ORDER_ACCEPTED => Lang::get('order_accepted'),
            ORDER_SHIPPED => Lang::get('order_shipped'),
            ORDER_FINISHED => Lang::get('order_finished'),
            ORDER_CANCELED => Lang::get('order_canceled'),
        );
        //导出Excel
        if ($_GET['exporttype'] == 'excel') {
            //获取数据
            $goods_list = $order_goods_mod->find(array(
                'conditions' => $conditions,
                'order' => 'add_time desc',
                'join' => 'belongs_to_order',
            ));

            //导出Excel
            import('excel.lib');
            $excel_obj = new Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
            //header
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '商品名称');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '店铺名称');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '产品单价');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '产品数量');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '下单时间');
            $excel_data[0][] = array('styleid' => 's_title', 'data' => '产品状态');
            //data
            foreach ($goods_list as $k => $v) {
                $excel_data[$k + 1][] = array('data' => $v['goods_name']);
                $excel_data[$k + 1][] = array('data' => $v['seller_name']);
                $excel_data[$k + 1][] = array('data' => $v['price']);
                $excel_data[$k + 1][] = array('data' => $v['quantity']);
                $excel_data[$k + 1][] = array('data' => date("Y-m-d H:i:s", $v['add_time']));
                $excel_data[$k + 1][] = array('data' => $order_status_list[$v['status']]);
            }
            $excel_data = $excel_obj->charset($excel_data, CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset('商品销售明细', CHARSET));
            $excel_obj->generateXML($excel_obj->charset('商品销售明细', CHARSET) . date('Y-m-d-H', time()));
            exit();
        } else {

            $this->assign('order_status_list', $order_status_list);

            $page = $this->_get_page(15);
            $goods_list = $order_goods_mod->find(array(
                'conditions' => $conditions,
                'order' => 'add_time desc',
                'join' => 'belongs_to_order',
                'limit' => $page['limit'],
                'count' => true,
            ));
            foreach ($goods_list as $key => $value) {
                $goods_list[$key]['status'] = $order_status_list[$value['status']];
            }
            $page['item_count'] = $order_goods_mod->getCount();
            $this->_format_page($page);
            $this->assign('page_info', $page);
            $this->assign('goods_list', $goods_list);

            $this->_curmenu('stat_trade_goods_sale');
            $this->display('stat_trade.goods_sale.html');
        }
    }

    function _get_member_submenu() {
        $submenus = array(
            array(
                'name' => 'stat_trade_goods',
                'url' => 'index.php?app=stat_trade&amp;act=goods',
            ),
            array(
                'name' => 'stat_trade_account',
                'url' => 'index.php?app=stat_trade&amp;act=account',
            ),
            array(
                'name' => 'stat_trade_goods_sale',
                'url' => 'index.php?app=stat_trade&amp;act=goods_sale',
            ),
            array(
                'name' => 'stat_trade_sale',
                'url' => 'index.php?app=stat_trade&amp;act=sale',
            ),
        );
        return $submenus;
    }

}

?>
